package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.CacReturnDto;
import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.CocReturnDto;
import com.csun.cmny.provider.pojo.CacOldmanMinor;
import com.csun.cmny.provider.pojo.CacOldmanMinorList;
import com.csun.cmny.provider.pojo.CacOldmanMinorPojo;
import com.csun.cmny.provider.service.CacOldmanMinorService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 * 社区老人选择的小项关系表
 * */
@RestController
@RequestMapping("/CacOldmanMinor")
public class CacOldmanMinorController extends BaseController {
    @Autowired
    private CacOldmanMinorService cacOldmanMinorService;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * 1.判断该用户是否属于该机构人员
     * */
    //添加老人小项数据
    @RequestMapping(value = "/addCacOldMnr", method = RequestMethod.POST)
    public Wrapper addCacOldMnr(@RequestBody List<CocReturnDto> list) {

        List<CacOldmanMinorPojo> arrayList = new ArrayList<>();

        if (list.size() <= 0 || list == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "请求数据异常！", null);
        }

        Date date = new Date();
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        list.stream().forEach(type -> {
            type.getBigItemList().stream().forEach(big -> {
                big.getMinorItemList().stream().forEach(min -> {
                    CacOldmanMinorPojo pojo = new CacOldmanMinorPojo();
                    pojo.setCreateTime(date);
                    pojo.setModifyTime(date);
                    pojo.setModifier(loginAuthDto.getUserId());
                    pojo.setGroupId(loginAuthDto.getGroupId());//添加组织机构ID
                    pojo.setGrade(min.getGrade());
                    pojo.setBaseTypeId(type.getBaseTypeId());
                    pojo.setBaseTypeName(type.getBaseTypeName());
                    pojo.setBigItemId(big.getBigItemId());
                    pojo.setBigItemName(big.getBigItemName());
                    pojo.setMinorItemId(min.getMinorItemId());
                    pojo.setMinorItemName(min.getMinorItemName());
                    pojo.setOldmanId(type.getOldmanId());
                    pojo.setCacId(type.getCacId());
                    arrayList.add(pojo);
                });
            });
        });

        cacOldmanMinorService.newInsertBatch(arrayList);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", null);
    }

    //修改老人小项数据
    @RequestMapping(value = "/updateCacOldMnr", method = RequestMethod.POST)
    public Wrapper updateCacOldMnr(@RequestBody CacOldmanMinorList cacOldmanMinorList) {
        //校验
        if (cacOldmanMinorList == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "对象不能为空", null);
        }
        List<CacOldmanMinor> cacOldmanMinors = cacOldmanMinorList.getCacOldmanMinors();
        Long groupId = getLoginAuthDto().getGroupId();
        if (CollectionUtils.isEmpty(cacOldmanMinors)) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "老人小项集合不能为空", null);
        }
        for (int i = 0; i < cacOldmanMinors.size(); i++) {
            CacOldmanMinor cacOldmanMinor = cacOldmanMinors.get(i);
            if (cacOldmanMinor.getMinorItemId() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "minorItemId不能为空", null);
            }
            if (cacOldmanMinor.getOldmanId() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "oldmanId不能为空", null);
            }
            if (cacOldmanMinor.getCacId() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "cacId不能为空", null);
            }
            if (cacOldmanMinor.getGrade() == null) {
                return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "grade不能为空", null);
            }
            cacOldmanMinor.setGroupId(groupId);//添加组织机构ID
        }
        Integer cacId = cacOldmanMinors.get(0).getCacId();
        Integer oldmanId = cacOldmanMinors.get(0).getOldmanId();
        //根据老人id和评估id删除之前插入的数据
        CacOldmanMinor cacOldmanMinor = new CacOldmanMinor();
        cacOldmanMinor.setCacId(cacId);
        cacOldmanMinor.setOldmanId(oldmanId);
        cacOldmanMinor.setGroupId(groupId);//添加组织机构ID
        int n = cacOldmanMinorService.delByOidAndCacId(cacOldmanMinor);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", null);
        }
        //批量插入
        int m = cacOldmanMinorService.insertBatch(cacOldmanMinors);
        if (m <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", null);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "修改成功", m);
    }

    /**
     * 根据老人id和评估id查询老人小项数据
     */
    @RequestMapping(value = "/selectCacOldMnr", method = RequestMethod.POST)
    public Wrapper selectCacOldMnr(CacOldmanMinor cacOldmanMinor) {
        //校验
        if (cacOldmanMinor.getOldmanId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "oldmanId不能为空", null);
        }
        List<CacReturnDto> cocReturnDtos = cacOldmanMinorService.selectByOldmanId(cacOldmanMinor.getOldmanId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cocReturnDtos);
    }
}
